package com.lg.erp.controller.maxuejiao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.maxuejiao.TPrinter;
import com.lg.erp.service.maxuejiao.IJGDYService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;


@RestController
public class JGDYController {
    @Autowired
    private IJGDYService ijgdyService;
    //分页查询
    @RequestMapping("/dyjs/{current}")/*/{current}*/
    public JsonResult selectAllPrinter(@PathVariable Integer current){
        try {
            System.out.println("selectAllPrinter");
            Page<TPrinter> dyjPage = new Page<>(current,5);
            IPage<TPrinter> dyjPages=ijgdyService.selectDyjPage(dyjPage);
            return new JsonResult(1, "ok", dyjPages);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(0,"error",null);
    }
    //添加
    @PostMapping("/dyjAdd")/*value = "/dyjAdd",method = RequestMethod.POST*/
    public JsonResult addOne(@RequestBody TPrinter dyj){
        System.out.println("addOne");
        dyj.setId(UUID.randomUUID().toString().replace("-","").substring(0,8));
        System.out.println(dyj.toString());
        ijgdyService.save(dyj);
        return new JsonResult(1,"ok",null);
    }
    //删除
    @RequestMapping("/dyjDel/{dyjId}")
    public JsonResult DeleteOne(@PathVariable String dyjId){
        System.out.println("DeleteOne");
        ijgdyService.removeById(dyjId);
        return new JsonResult(1,"ok",null);
    }
    //更新
    @PostMapping("update")
    public JsonResult update(@RequestBody TPrinter dyj){
        System.out.println("update");
        ijgdyService.updateById(dyj);
        return new JsonResult(1,"ok",null);
    }
    //查询某个
    @GetMapping("/selectDyj/{pNum}")
    public JsonResult selectById(@PathVariable String pNum){
        TPrinter printer = ijgdyService.getOne(new QueryWrapper<TPrinter>()
                .lambda()
                .eq(TPrinter::getPNum, pNum));
        return new JsonResult(1,"ok",printer);
    }
}
